const uuid = require('uuid')

// 创建巡检数据字典编码表
function createMRiCode() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_code("cd_id" TEXT PRIMARY KEY,"cat_no" TEXT,"cd_no" TEXT,"cd_name" TEXT,"sort" INTEGER)',
		success(e) {
			console.log("创建表【m_ri_code】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_code】失败:", e)
		}
	})
}

// 创建测点数据表
function createMEqMpData() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_eq_mp_data("mp_data_id" TEXT PRIMARY KEY,"mp_no" TEXT,"data_time" TEXT,"data_value" TEXT,"extend" TEXT)',
		success(e) {
			console.log("创建表【m_eq_mp_data】成功")
		},
		fail(e) {
			console.log("创建表【m_eq_mp_data】失败:", e)
		}
	})
}

// 创建路线危险源表
function createMRiLineHazard() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_line_hazard("line_hazard_id" TEXT PRIMARY KEY,"line_id" TEXT,"hazard" TEXT,"pvn_measure" TEXT,"sort_no" INTEGER)',
		success(e) {
			console.log("创建表【m_ri_line_hazard】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_line_hazard】失败:", e)
		}
	})
}

// 创建巡检任务表
function createMRiTask() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task("task_id" TEXT PRIMARY KEY,"line_id" TEXT,"task_name" TEXT,"work_dept_no" TEXT,"work_dept_name" TEXT,"task_type" TEXT,"plan_start_time" TEXT,"plan_end_time" TEXT,"preparing" TEXT,"actual_staff_id" TEXT,"actual_staff_name" TEXT,"actual_start_time" TEXT,"actual_end_time" TEXT,"finish_report" TEXT,"label_type" TEXT,"is_area_location" INTEGER,"is_scan_hand" INTEGER,"task_state" INTEGER,"update_time" TEXT,"attach_file" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task】失败:", e)
		}
	})
}

// 创建任务检查项表
function createMRiTaskInspection() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_inspection("task_insp_id" TEXT PRIMARY KEY,"task_id" TEXT,"line_id" TEXT,"area_id" TEXT,"area_no" TEXT,"area_name" TEXT,"area_sort_no" INTEGER,"loca_id" TEXT,"loca_no" TEXT,"loca_name" TEXT,"loca_sort_no" INTEGER,"asset_id" TEXT,"asset_no" TEXT,"asset_name" TEXT,"insp_sort_no" INTEGER,"insp_part" TEXT,"insp_content" TEXT,"insp_standard" TEXT,"insp_method" INTEGER,"insp_loca_status" TEXT,"judge_method" INTEGER,"exec_result" INTEGER,"exec_loca_status" INTEGER,"exec_time" TEXT,"exec_status" INTEGER,"update_time" TEXT,"complete_tm" TEXT,"exec_user_id" TEXT,"exec_user_name" TEXT,"line_insp_id" TEXT,"attach_file" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task_inspection】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_inspection】失败:", e)
		}
	})
}

// 创建巡检任务补充信息表
function createMRiTaskInfo() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_info("task_id" TEXT PRIMARY KEY,"is_efi" INTEGER,"efi_num" INTEGER,"efi_reason" TEXT,"apply_time" TEXT,"apply_state" INTEGER)',
		success(e) {
			console.log("创建表【m_ri_task_info】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_info】失败:", e)
		}
	})
}

// 创建路线检查项测点表
function createMRiLineInspMp() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_line_insp_mp("id" TEXT PRIMARY KEY,"line_id" TEXT,"line_insp_id" TEXT,"mp_id" TEXT,"mp_no" TEXT,"mp_name" TEXT,"mp_type" TEXT,"mp_mm" INTEGER,"unit" TEXT,"range_standard" TEXT,"range_top" TEXT,"range_bottom" TEXT,"parm_value_type_no" TEXT,"decimal_digit" INTEGER,"loca_id" TEXT,"loca_no" TEXT,"loca_name" TEXT,"asset_id" TEXT,"asset_no" TEXT,"asset_name" TEXT)',
		success(e) {
			console.log("创建表【m_ri_line_insp_mp】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_line_insp_mp】失败:", e)
		}
	})
}

// 创建任务检查项测点表
function createMRiTaskInspMp() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_insp_mp("id" TEXT PRIMARY KEY,"task_id" TEXT,"line_id" TEXT,"task_insp_id" TEXT,"mp_id" TEXT,"mp_no" TEXT,"mp_name" TEXT,"mp_type" TEXT,"mp_mm" INTEGER,"unit" TEXT,"range_standard" TEXT,"range_top" TEXT,"range_bottom" TEXT,"parm_value_type_no" TEXT,"decimal_digit" INTEGER,"exec_value" TEXT,"exec_result" INTEGER,"loca_id" TEXT,"loca_no" TEXT,"loca_name" TEXT,"asset_id" TEXT,"asset_no" TEXT,"asset_name" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task_insp_mp】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_insp_mp】失败:", e)
		}
	})
}

// 创建任务检查项异常情况表
function createMRiTaskInspectionAbnormal() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_inspection_abnormal("id" TEXT PRIMARY KEY,"task_id" TEXT,"task_insp_id" TEXT,"loca_id" TEXT,"loca_no" TEXT,"loca_name" TEXT,"asset_id" TEXT,"asset_no" TEXT,"asset_name" TEXT,"insp_part" TEXT,"insp_content" TEXT,"insp_standard" TEXT,"insp_method" INTEGER,"abnormal_memo" TEXT,"exec_loca_status" INTEGER,"exec_time" TEXT,"is_abnormal_handled" INTEGER,"handling_memo" TEXT,"mp_id" TEXT,"df_no" TEXT,"create_time" TEXT,"update_time" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task_inspection_abnormal】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_inspection_abnormal】失败:", e)
		}
	})
}

// 创建任务离线下载表
function createMRiTaskDown() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_down("id" TEXT PRIMARY KEY,"task_id" TEXT,"node_id" TEXT,"node_name" TEXT,"seq" INTEGER,"down_state" TEXT,"down_time" TEXT,"create_time" TEXT,"update_time" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task_down】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_down】失败:", e)
		}
	})
}

// 创建任务上传表
function createMRiTaskUpload() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_ri_task_upload("id" TEXT PRIMARY KEY,"task_id" TEXT,"node_id" TEXT,"node_name" TEXT,"seq" INTEGER,"upload_state" TEXT,"upload_time" TEXT,"create_time" TEXT,"update_time" TEXT)',
		success(e) {
			console.log("创建表【m_ri_task_upload】成功")
		},
		fail(e) {
			console.log("创建表【m_ri_task_upload】失败:", e)
		}
	})
}

// 创建设备采集状况登记表
function createMCpisAppCollectStatus() {
	plus.sqlite.executeSql({
		name: 'cpis',
		sql: 'create table if not exists m_cpis_app_collect_status("collect_id" TEXT PRIMARY KEY,"link_type" INTEGER,"eq_code" TEXT,"item_type" TEXT,"collect_state" INTEGER,"blue_link_result" TEXT,"begin_collect_time" TEXT,"end_collect_time" TEXT,"token" TEXT,"serial_no" TEXT,"report_user" TEXT,"report_user_name" TEXT,"company_id" TEXT,"create_user" TEXT,"create_time" TEXT,"update_user" TEXT,"update_time" TEXT)',
		success(e) {
			console.log("创建表【m_cpis_app_collect_status】成功")
		},
		fail(e) {
			console.log("创建表【m_cpis_app_collect_status】失败:", e)
		}
	})
}

// 向巡检数据字典编码表里添加数据
function insertMRiCode(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var cdId = obj.cdId;
			var catNo = obj.catNo; 
			var cdNo = obj.cdNo; 
			var cdName = obj.cdName; 
			var sort = obj.sort; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_code(cd_id,cat_no,cd_no,cd_name,sort) values("' + 
					cdId + '","' + catNo + '","' + cdNo + '","' + cdName + '","' + sort + '")',
				success(e) {
					console.log("表【m_ri_code】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_code】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_code】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_code】数据插入失败:参数为空");
	}
}

// 批量向巡检数据字典编码表里添加数据
function insertMRiCodeBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var cdId = obj.cdId;
				var catNo = obj.catNo; 
				var cdNo = obj.cdNo; 
				var cdName = obj.cdName; 
				var sort = obj.sort; 
				
				let sql = 'insert into m_ri_code(cd_id,cat_no,cd_no,cd_name,sort) values("' + 
					cdId + '","' + catNo + '","' + cdNo + '","' + cdName + '","' + sort + '")';
				
				sqls.push(sql)
			} else {
				console.log("表【m_ri_code】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_ri_code】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_ri_code】数据插入成功");
			},
			fail(e) {
				console.log("表【m_ri_code】数据插入失败:", e);
			}
		})
	}
}

// 向测点数据表里添加数据
function insertMEqMpData(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var mpDataId = obj.mpDataId || uuid(); 
			var mpNo = obj.mpNo;
			var dataTime = obj.dataTime; 
			var dataValue = obj.dataValue; 
			var extend = obj.extend; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_eq_mp_data(mp_data_id,mp_no,data_time,data_value,extend) values("' + 
					mpDataId + '","' + mpNo + '","' + dataTime + '","' + dataValue + '","' + extend + '")',
				success(e) {
					console.log("表【m_eq_mp_data】数据插入成功");
				},
				fail(e) {
					console.log("表【m_eq_mp_data】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_eq_mp_data】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_eq_mp_data】数据插入失败:参数为空");
	}
}

// 批量向测点数据表里添加数据
function insertMEqMpDataBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var mpDataId = obj.mpDataId || uuid(); 
				var mpNo = obj.mpNo;
				var dataTime = obj.dataTime; 
				var dataValue = obj.dataValue; 
				var extend = obj.extend; 
				
				let sql = 'insert into m_eq_mp_data(mp_data_id,mp_no,data_time,data_value,extend) values("' + 
					mpDataId + '","' + mpNo + '","' + dataTime + '","' + dataValue + '","' + extend + '")';
				
				sqls.push(sql)
			} else {
				console.log("表【m_eq_mp_data】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_eq_mp_data】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_eq_mp_data】数据插入成功");
			},
			fail(e) {
				console.log("表【m_eq_mp_data】数据插入失败:", e);
			}
		})
	}
}

// 向路线危险源表里添加数据
function insertMRiLineHazard(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var lineHazardId = obj.lineHazardId; 
			var lineId = obj.lineId; 
			var hazard = obj.hazard; 
			var pvnMeasure = obj.pvnMeasure; 
			var sortNo = obj.sortNo; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_line_hazard(line_hazard_id,line_id,hazard,pvn_measure,sort_no) values("' + 
					lineHazardId + '","' + lineId + '","' + hazard + '","' + pvnMeasure + '","' + sortNo + '")',
				success(e) {
					console.log("表【m_ri_line_hazard】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_line_hazard】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_line_hazard】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_line_hazard】数据插入失败:参数为空");
	}
}

// 向巡检任务表里添加数据
function insertMRiTask(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var taskId = obj.taskId; 
			var lineId = obj.lineId; 
			var taskName = obj.taskName;
			var workDeptNo = obj.workDeptNo;
			var workDeptName = obj.workDeptName;
			var taskType = obj.taskType; 
			var planStartTime = obj.planStartTime; 
			var planEndTime = obj.planEndTime; 
			var preparing = obj.preparing;
			var actualStaffId = obj.actualStaffId; 
			var actualStaffName = obj.actualStaffName; 
			var actualStartTime = obj.actualStartTime; 
			var actualEndTime = obj.actualEndTime;
			var finishReport = obj.finishReport; 
			var labelType = obj.labelType; 
			var isAreaLocation = obj.isAreaLocation; 
			var isScanHand = obj.isScanHand;
			var taskState = obj.taskState; 
			var updateTime = obj.updateTime; 
			var attachFile = obj.attachFile; 

			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task(task_id,line_id,task_name,work_dept_no,work_dept_name,task_type,plan_start_time,plan_end_time,preparing,actual_staff_id,actual_staff_name,actual_start_time,actual_end_time,finish_report,label_type,is_area_location,is_scan_hand,task_state,update_time,attach_file) values("' + 
					taskId + '","' + lineId + '","' + taskName + '","' + workDeptNo + '","' + workDeptName + '","' + taskType + '","' + planStartTime + '","' + planEndTime + '","' + preparing + '","' + actualStaffId + '","' + actualStaffName + '","' + actualStartTime + '","' + actualEndTime + '","' + finishReport + '","' + labelType + '","' + isAreaLocation + '","' + isScanHand + '","' + taskState + '","' + updateTime + '","' + attachFile + '")',
				success(e) {
					console.log("表【m_ri_task】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task】数据插入失败:参数为空");
	}
}

// 向任务检查项表里添加数据
function insertMRiTaskInspection(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var taskInspId = obj.taskInspId; 
			var taskId = obj.taskId; 
			var lineId = obj.lineId; 
			var areaId = obj.areaId; 
			var areaNo = obj.areaNo;
			var areaName = obj.areaName; 
			var areaSortNo = obj.areaSortNo; 
			var locaId = obj.locaId; 
			var locaNo = obj.locaNo;
			var locaName = obj.locaName; 
			var locaSortNo = obj.locaSortNo; 
			var assetId = obj.assetId; 
			var assetNo = obj.assetNo; 
			var assetName = obj.assetName;
			var inspSortNo = obj.inspSortNo; 
			var inspPart = obj.inspPart; 
			var inspContent = obj.inspContent;
			var inspStandard = obj.inspStandard; 
			var inspMethod = obj.inspMethod; 
			var inspLocaStatus = obj.inspLocaStatus; 
			var judgeMethod = obj.judgeMethod;
			var execResult = obj.execResult; 
			var execLocaStatus = obj.execLocaStatus; 
			var execTime = obj.execTime; 
			var execStatus = obj.execStatus;
			var updateTime = obj.updateTime;
			var completeTm = obj.completeTm; 
			var execUserId = obj.execUserId;
			var execUserName = obj.execUserName; 
			var lineInspId = obj.lineInspId; 
			var attachFile = obj.attachFile; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_inspection(task_insp_id,task_id,line_id,area_id,area_no,area_name,area_sort_no,loca_id,loca_no,loca_name,loca_sort_no,asset_id,asset_no,asset_name,insp_sort_no,insp_part,insp_content,insp_standard,insp_method,insp_loca_status,judge_method,exec_result,exec_loca_status,exec_time,exec_status,update_time,complete_tm,exec_user_id,exec_user_name,line_insp_id,attach_file) values("' + 
					taskInspId + '","' + taskId + '","' + lineId + '","' + areaId + '","' + areaNo + '","' + areaName + '","' + areaSortNo + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + locaSortNo + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + inspSortNo + '","' + inspPart + '","' + inspContent + '","' + inspStandard + '","' + inspMethod + '","' + inspLocaStatus + '","' + judgeMethod + '","' + execResult + '","' + execLocaStatus + '","' + execTime + '","' + execStatus + '","' + updateTime + '","' + completeTm + '","' + execUserId + '","' + execUserName + '","' + lineInspId + '","' + attachFile + '")',
				success(e) {
					console.log("表【m_ri_task_inspection】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_inspection】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_inspection】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_inspection】数据插入失败:参数为空");
	}
}

// 向任务检查项表里添加数据
function insertMRiTaskInspectionBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var taskInspId = obj.taskInspId; 
				var taskId = obj.taskId; 
				var lineId = obj.lineId; 
				var areaId = obj.areaId; 
				var areaNo = obj.areaNo;
				var areaName = obj.areaName; 
				var areaSortNo = obj.areaSortNo; 
				var locaId = obj.locaId; 
				var locaNo = obj.locaNo;
				var locaName = obj.locaName; 
				var locaSortNo = obj.locaSortNo; 
				var assetId = obj.assetId; 
				var assetNo = obj.assetNo; 
				var assetName = obj.assetName;
				var inspSortNo = obj.inspSortNo; 
				var inspPart = obj.inspPart; 
				var inspContent = obj.inspContent;
				var inspStandard = obj.inspStandard; 
				var inspMethod = obj.inspMethod; 
				var inspLocaStatus = obj.inspLocaStatus; 
				var judgeMethod = obj.judgeMethod;
				var execResult = obj.execResult; 
				var execLocaStatus = obj.execLocaStatus; 
				var execTime = obj.execTime; 
				var execStatus = obj.execStatus;
				var updateTime = obj.updateTime;
				var completeTm = obj.completeTm; 
				var execUserId = obj.execUserId;
				var execUserName = obj.execUserName; 
				var lineInspId = obj.lineInspId; 
				var attachFile = obj.attachFile;
				
				let sql = 'insert into m_ri_task_inspection(task_insp_id,task_id,line_id,area_id,area_no,area_name,area_sort_no,loca_id,loca_no,loca_name,loca_sort_no,asset_id,asset_no,asset_name,insp_sort_no,insp_part,insp_content,insp_standard,insp_method,insp_loca_status,judge_method,exec_result,exec_loca_status,exec_time,exec_status,update_time,complete_tm,exec_user_id,exec_user_name,line_insp_id,attach_file) values("' + 
					taskInspId + '","' + taskId + '","' + lineId + '","' + areaId + '","' + areaNo + '","' + areaName + '","' + areaSortNo + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + locaSortNo + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + inspSortNo + '","' + inspPart + '","' + inspContent + '","' + inspStandard + '","' + inspMethod + '","' + inspLocaStatus + '","' + judgeMethod + '","' + execResult + '","' + execLocaStatus + '","' + execTime + '","' + execStatus + '","' + updateTime + '","' + completeTm + '","' + execUserId + '","' + execUserName + '","' + lineInspId + '","' + attachFile + '")';
					
				sqls.push(sql)
			} else {
				console.log("表【m_ri_task_inspection】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_ri_task_inspection】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_ri_task_inspection】数据插入成功");
			},
			fail(e) {
				console.log("表【m_ri_task_inspection】数据插入失败:", e);
			}
		})
	}
}

// 向巡检任务补充信息表里添加数据
function insertMRiTaskInfo(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var taskId = obj.taskId; 
			var isEfi = obj.isEfi; 
			var efiNum = obj.efiNum; 
			var efiReason = obj.efiReason; 
			var applyTime = obj.applyTime;
			var applyState = obj.applyState;
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_info(task_id,is_efi,efi_num,efi_reason,apply_time,apply_state) values("' + 
					taskId + '","' + isEfi + '","' + efiNum + '","' + efiReason + '","' + applyTime + '","' + applyState + '")',
				success(e) {
					console.log("表【m_ri_task_info】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_info】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_info】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_info】数据插入失败:参数为空");
	}
}

// 向路线检查项测点表里添加数据
function insertMRiLineInspMp(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var id = obj.id; 
			var lineId = obj.lineId; 
			var lineInspId = obj.lineInspId; 
			var mpId = obj.mpId; 
			var mpNo = obj.mpNo; 
			var mpName = obj.mpName; 
			var mpType = obj.mpType; 
			var mpMm = obj.mpMm; 
			var unit = obj.unit;
			var rangeStandard = obj.rangeStandard; 
			var rangeTop = obj.rangeTop; 
			var rangeBottom = obj.rangeBottom; 
			var parmValueTypeNo = obj.parmValueTypeNo;
			var decimalDigit = obj.decimalDigit; 
			var locaId = obj.locaId; 
			var locaNo = obj.locaNo;
			var locaName = obj.locaName; 
			var assetId = obj.assetId; 
			var assetNo = obj.assetNo; 
			var assetName = obj.assetName;

			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_line_insp_mp(id,line_id,line_insp_id,mp_id,mp_no,mp_name,mp_type,mp_mm,unit,range_standard,range_top,range_bottom,parm_value_type_no,decimal_digit,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name) values("' + 
					id + '","' + lineId + '","' + lineInspId + '","' + mpId + '","' + mpNo + '","' + mpName + '","' + mpType + '","' + mpMm + '","' + unit + '","' + rangeStandard + '","' + rangeTop + '","' + rangeBottom + '","' + parmValueTypeNo + '","' + decimalDigit + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '")',
				success(e) {
					console.log("表【m_ri_line_insp_mp】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_line_insp_mp】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_line_insp_mp】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_line_insp_mp】数据插入失败:参数为空");
	}
}

// 批量向路线检查项测点表里添加数据
function insertMRiLineInspMpBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var id = obj.id; 
				var lineId = obj.lineId; 
				var lineInspId = obj.lineInspId; 
				var mpId = obj.mpId; 
				var mpNo = obj.mpNo; 
				var mpName = obj.mpName; 
				var mpType = obj.mpType; 
				var mpMm = obj.mpMm; 
				var unit = obj.unit;
				var rangeStandard = obj.rangeStandard; 
				var rangeTop = obj.rangeTop; 
				var rangeBottom = obj.rangeBottom; 
				var parmValueTypeNo = obj.parmValueTypeNo;
				var decimalDigit = obj.decimalDigit; 
				var locaId = obj.locaId; 
				var locaNo = obj.locaNo;
				var locaName = obj.locaName; 
				var assetId = obj.assetId; 
				var assetNo = obj.assetNo; 
				var assetName = obj.assetName;
				
				let sql = 'insert into m_ri_line_insp_mp(id,line_id,line_insp_id,mp_id,mp_no,mp_name,mp_type,mp_mm,unit,range_standard,range_top,range_bottom,parm_value_type_no,decimal_digit,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name) values("' + 
					id + '","' + lineId + '","' + lineInspId + '","' + mpId + '","' + mpNo + '","' + mpName + '","' + mpType + '","' + mpMm + '","' + unit + '","' + rangeStandard + '","' + rangeTop + '","' + rangeBottom + '","' + parmValueTypeNo + '","' + decimalDigit + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '")';
				
				sqls.push(sql)
			} else {
				console.log("表【m_ri_line_insp_mp】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_ri_line_insp_mp】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_ri_line_insp_mp】数据插入成功");
			},
			fail(e) {
				console.log("表【m_ri_line_insp_mp】数据插入失败:", e);
			}
		})
	}
}

// 向任务检查项测点表里添加数据
function insertMRiTaskInspMp(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var id = obj.id || uuid(); 
			var taskId = obj.taskId; 
			var lineId = obj.lineId; 
			var taskInspId = obj.taskInspId; 
			var mpId = obj.mpId; 
			var mpNo = obj.mpNo; 
			var mpName = obj.mpName; 
			var mpType = obj.mpType; 
			var mpMm = obj.mpMm; 
			var unit = obj.unit;
			var rangeStandard = obj.rangeStandard; 
			var rangeTop = obj.rangeTop; 
			var rangeBottom = obj.rangeBottom; 
			var parmValueTypeNo = obj.parmValueTypeNo;
			var decimalDigit = obj.decimalDigit; 
			var execValue = obj.execValue; 
			var execResult = obj.execResult; 
			var locaId = obj.locaId; 
			var locaNo = obj.locaNo;
			var locaName = obj.locaName; 
			var assetId = obj.assetId; 
			var assetNo = obj.assetNo; 
			var assetName = obj.assetName;

			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_insp_mp(id,task_id,line_id,task_insp_id,mp_id,mp_no,mp_name,mp_type,mp_mm,unit,range_standard,range_top,range_bottom,parm_value_type_no,decimal_digit,exec_value,exec_result,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name) values("' + 
					id + '","' + taskId + '","' + lineId + '","' + taskInspId + '","' + mpId + '","' + mpNo + '","' + mpName + '","' + mpType + '","' + mpMm + '","' + unit + '","' + rangeStandard + '","' + rangeTop + '","' + rangeBottom + '","' + parmValueTypeNo + '","' + decimalDigit + '","' + execValue + '","' + execResult + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '")',
				success(e) {
					console.log("表【m_ri_task_insp_mp】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_insp_mp】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_insp_mp】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_insp_mp】数据插入失败:参数为空");
	}
}

// 批量向任务检查项测点表里添加数据
function insertMRiTaskInspMpBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var id = obj.id || uuid(); 
				var taskId = obj.taskId; 
				var lineId = obj.lineId; 
				var taskInspId = obj.taskInspId; 
				var mpId = obj.mpId; 
				var mpNo = obj.mpNo; 
				var mpName = obj.mpName; 
				var mpType = obj.mpType; 
				var mpMm = obj.mpMm; 
				var unit = obj.unit;
				var rangeStandard = obj.rangeStandard; 
				var rangeTop = obj.rangeTop; 
				var rangeBottom = obj.rangeBottom; 
				var parmValueTypeNo = obj.parmValueTypeNo;
				var decimalDigit = obj.decimalDigit; 
				var execValue = obj.execValue; 
				var execResult = obj.execResult; 
				var locaId = obj.locaId; 
				var locaNo = obj.locaNo;
				var locaName = obj.locaName; 
				var assetId = obj.assetId; 
				var assetNo = obj.assetNo; 
				var assetName = obj.assetName;
				
				let sql = 'insert into m_ri_task_insp_mp(id,task_id,line_id,task_insp_id,mp_id,mp_no,mp_name,mp_type,mp_mm,unit,range_standard,range_top,range_bottom,parm_value_type_no,decimal_digit,exec_value,exec_result,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name) values("' +
					id + '","' + taskId + '","' + lineId + '","' + taskInspId + '","' + mpId + '","' + mpNo + '","' + mpName + '","' + mpType + '","' + mpMm + '","' + unit + '","' + rangeStandard + '","' + rangeTop + '","' + rangeBottom + '","' + parmValueTypeNo + '","' + decimalDigit + '","' + execValue + '","' + execResult + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '")';
				
				sqls.push(sql)
			} else {
				console.log("表【m_ri_task_insp_mp】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_ri_task_insp_mp】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_ri_task_insp_mp】数据插入成功");
			},
			fail(e) {
				console.log("表【m_ri_task_insp_mp】数据插入失败:", e);
			}
		})
	}
}

// 向任务检查项异常情况表里添加数据
function insertMRiTaskInspectionAbnormal(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var id = obj.id || uuid(); 
			var taskId = obj.taskId; 
			var taskInspId = obj.taskInspId; 
			var locaId = obj.locaId; 
			var locaNo = obj.locaNo;
			var locaName = obj.locaName; 
			var assetId = obj.assetId; 
			var assetNo = obj.assetNo; 
			var assetName = obj.assetName; 
			var inspPart = obj.inspPart; 
			var inspContent = obj.inspContent;
			var inspStandard = obj.inspStandard; 
			var inspMethod = obj.inspMethod; 
			var abnormalMemo = obj.abnormalMemo; 
			var execLocaStatus = obj.execLocaStatus; 
			var execTime = obj.execTime; 
			var isAbnormalHandled = obj.isAbnormalHandled;
			var handlingMemo = obj.handlingMemo; 
			var mpId = obj.mpId;
			var dfNo = obj.dfNo; 
			var createTime = obj.createTime; 
			var updateTime = obj.updateTime;

			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_inspection_abnormal(id,task_id,task_insp_id,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name,insp_part,insp_content,insp_standard,insp_method,abnormal_memo,exec_loca_status,exec_time,is_abnormal_handled,handling_memo,mp_id,df_no,create_time,update_time) values("' + 
					id + '","' + taskId + '","' + taskInspId + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + inspPart + '","' + inspContent + '","' + inspStandard + '","' + inspMethod + '","' + abnormalMemo + '","' + execLocaStatus + '","' + execTime + '","' + isAbnormalHandled + '","' + handlingMemo + '","' + mpId + '","' + dfNo + '","' + createTime + '","' + updateTime + '")',
				success(e) {
					console.log("表【m_ri_task_inspection_abnormal】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_inspection_abnormal】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_inspection_abnormal】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_inspection_abnormal】数据插入失败:参数为空");
	}
}

// 批量向任务检查项异常情况表里添加数据
function insertMRiTaskInspectionAbnormalBatch(arr) {
	let sqls = []
	arr.forEach(obj => {
		//判断有没有传参
		if (obj !== undefined) {
			//判断传的参是否有值
			var b = (JSON.stringify(obj) == "{}");
			if (!b) {
				var id = obj.id || uuid(); 
				var taskId = obj.taskId; 
				var taskInspId = obj.taskInspId; 
				var locaId = obj.locaId; 
				var locaNo = obj.locaNo;
				var locaName = obj.locaName; 
				var assetId = obj.assetId; 
				var assetNo = obj.assetNo; 
				var assetName = obj.assetName; 
				var inspPart = obj.inspPart; 
				var inspContent = obj.inspContent;
				var inspStandard = obj.inspStandard; 
				var inspMethod = obj.inspMethod; 
				var abnormalMemo = obj.abnormalMemo; 
				var execLocaStatus = obj.execLocaStatus; 
				var execTime = obj.execTime; 
				var isAbnormalHandled = obj.isAbnormalHandled;
				var handlingMemo = obj.handlingMemo; 
				var mpId = obj.mpId;
				var dfNo = obj.dfNo; 
				var createTime = obj.createTime; 
				var updateTime = obj.updateTime;
				
				let sql = 'insert into m_ri_task_inspection_abnormal(id,task_id,task_insp_id,loca_id,loca_no,loca_name,asset_id,asset_no,asset_name,insp_part,insp_content,insp_standard,insp_method,abnormal_memo,exec_loca_status,exec_time,is_abnormal_handled,handling_memo,mp_id,df_no,create_time,update_time) values("' +
					id + '","' + taskId + '","' + taskInspId + '","' + locaId + '","' + locaNo + '","' + locaName + '","' + assetId + '","' + assetNo + '","' + assetName + '","' + inspPart + '","' + inspContent + '","' + inspStandard + '","' + inspMethod + '","' + abnormalMemo + '","' + execLocaStatus + '","' + execTime + '","' + isAbnormalHandled + '","' + handlingMemo + '","' + mpId + '","' + dfNo + '","' + createTime + '","' + updateTime + '")';
				
				sqls.push(sql)
			} else {
				console.log("表【m_ri_task_inspection_abnormal】数据插入失败:参数为空");
			}
		} else {
			console.log("表【m_ri_task_inspection_abnormal】数据插入失败:参数为空");
		}
	})
	
	if(sqls.length > 0) {
		plus.sqlite.executeSql({
			name: 'cpis',
			sql: sqls,
			success(e) {
				console.log("表【m_ri_task_inspection_abnormal】数据插入成功");
			},
			fail(e) {
				console.log("表【m_ri_task_inspection_abnormal】数据插入失败:", e);
			}
		})
	}
}

// 向任务离线下载表里添加数据
function insertMRiTaskDown(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var taskId = obj.taskId; 
			var nodeId = obj.nodeId; 
			var nodeName = obj.nodeName; 
			var seq = obj.seq; 
			var downState = obj.downState;
			var downTime = obj.downTime; 
			var createTime = obj.createTime; 
			var updateTime = obj.updateTime; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_down(id,task_id,node_id,node_name,seq,down_state,down_time,create_time,update_time) values("' + 
					uuid() + '","' + taskId + '","' + nodeId + '","' + nodeName + '","' + seq + '","' + downState + '","' + downTime + '","' + createTime + '","' + updateTime + '")',
				success(e) {
					console.log("表【m_ri_task_down】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_down】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_down】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_down】数据插入失败:参数为空");
	}
}

// 向任务上传表里添加数据
function insertMRiTaskUpload(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var taskId = obj.taskId; 
			var nodeId = obj.nodeId; 
			var nodeName = obj.nodeName; 
			var seq = obj.seq; 
			var uploadState = obj.uploadState;
			var uploadTime = obj.uploadTime; 
			var createTime = obj.createTime; 
			var updateTime = obj.updateTime; 
			
			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_ri_task_upload(id,task_id,node_id,node_name,seq,upload_state,upload_time,create_time,update_time) values("' + 
					uuid() + '","' + taskId + '","' + nodeId + '","' + nodeName + '","' + seq + '","' + uploadState + '","' + uploadTime + '","' + createTime + '","' + updateTime + '")',
				success(e) {
					console.log("表【m_ri_task_upload】数据插入成功");
				},
				fail(e) {
					console.log("表【m_ri_task_upload】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_ri_task_upload】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_ri_task_upload】数据插入失败:参数为空");
	}
}

// 向设备采集状况登记表里添加数据
function insertMCpisAppCollectStatus(obj) {
	//判断有没有传参
	if (obj !== undefined) {
		//判断传的参是否有值
		var b = (JSON.stringify(obj) == "{}");
		if (!b) {
			var collectId = obj.collectId || uuid(); 
			var linkType = obj.linkType || 1; 
			var eqCode = obj.eqCode; 
			var itemType = obj.itemType;
			var collectState = obj.collectState;
			var blueLinkResult = obj.blueLinkResult;
			var beginCollectTime = obj.beginCollectTime; 
			var endCollectTime = obj.endCollectTime; 
			var token = obj.token;
			var serialNo = obj.serialNo; 
			var reportUser = obj.reportUser; 
			var reportUserName = obj.reportUserName; 
			var companyId = obj.companyId;
			var createUser = obj.createUser; 
			var createTime = obj.createTime; 
			var updateUser = obj.updateUser; 
			var updateTime = obj.updateTime;

			plus.sqlite.executeSql({
				name: 'cpis',
				sql: 'insert into m_cpis_app_collect_status(collect_id,link_type,eq_code,item_type,collect_state,blue_link_result,begin_collect_time,end_collect_time,token,serial_no,report_user,report_user_name,company_id,create_user,create_time,update_user,update_time) values("' + 
					collectId + '","' + linkType + '","' + eqCode + '","' + itemType + '","' + collectState + '","' + blueLinkResult + '","' + beginCollectTime + '","' + endCollectTime + '","' + token + '","' + serialNo + '","' + reportUser + '","' + reportUserName + '","' + companyId + '","' + createUser + '","' + createTime + '","' + updateUser + '","' + updateTime + '")',
				success(e) {
					console.log("表【m_cpis_app_collect_status】数据插入成功");
				},
				fail(e) {
					console.log("表【m_cpis_app_collect_status】数据插入失败:", e);
				}
			})
		} else {
			console.log("表【m_cpis_app_collect_status】数据插入失败:参数为空");
		}
	} else {
		console.log("表【m_cpis_app_collect_status】数据插入失败:参数为空");
	}
}

// 查询巡检任务列表
function taskPageTypeList(type, taskId) {
	if (type !== undefined && taskId !== undefined) {
		var sql
		if(type === 0) { // 设备
			sql = "select DISTINCT loca_id,loca_no,loca_name from m_ri_task_inspection where task_id = '" + taskId + "' order by loca_sort_no";
		} else if(type === 1) { // 区域
			sql = "select DISTINCT area_id,area_no,area_name from m_ri_task_inspection where task_id = '" + taskId + "' order by area_sort_no";
		}
		console.log("sql语句：" + sql)
		return new Promise((resolve, reject) => {
			plus.sqlite.selectSql({
				name: 'cpis',
				sql: sql,
				success(e) {
					resolve(e);
				},
				fail(e) {
					reject(e);
				}
			})
		})
	} else {
		return new Promise((resolve, reject) => {
			reject("错误查询")
		});
	}
}

// 查询巡检任务区域数量
function getTaskAreaStat(taskId, execStatus) {
	if (taskId !== undefined) { // 区域总数
		var sql = "select DISTINCT area_id from m_ri_task_inspection where task_id = '" + taskId + "'"
		if(execStatus !== undefined) { // 未到位区域
			sql += " and exec_status = '" + execStatus + "'";
		}
		console.log("sql语句：" + sql)
		return new Promise((resolve, reject) => {
			plus.sqlite.selectSql({
				name: 'cpis',
				sql: sql,
				success(e) {
					resolve(e);
				},
				fail(e) {
					reject(e);
				}
			})
		})
	} else {
		return new Promise((resolve, reject) => {
			reject("错误查询")
		});
	}
}

// 查询测点历史数据
function getInspectionHistory(mpNo) {
	if (mpNo !== undefined) {
		var sql = "select data_time, data_value from m_eq_mp_data where mp_no = '" + mpNo + "' order by data_time desc"
		
		console.log("sql语句：" + sql)
		return new Promise((resolve, reject) => {
			plus.sqlite.selectSql({
				name: 'cpis',
				sql: sql,
				success(e) {
					resolve(e);
				},
				fail(e) {
					reject(e);
				}
			})
		})
	} else {
		return new Promise((resolve, reject) => {
			reject("错误查询")
		});
	}
}

// 查询测点表数据
function getMpDataList(mpNo, startTime, endTime) {
	if (startTime !== undefined && endTime !== undefined) {
		var sql = "select * from m_eq_mp_data where data_time >= '" + startTime + "' and data_time <= '" + endTime + "' "
		
		if(mpNo !== undefined) {
			sql += "and mp_no = '" + mpNo + "' "
		}
		
		sql += "order by data_time desc"
		
		console.log("sql语句：" + sql)
		return new Promise((resolve, reject) => {
			plus.sqlite.selectSql({
				name: 'cpis',
				sql: sql,
				success(e) {
					resolve(e);
				},
				fail(e) {
					reject(e);
				}
			})
		})
	} else {
		return new Promise((resolve, reject) => {
			reject("错误查询")
		});
	}
}

export {
	createMRiCode,
	createMEqMpData,
	createMRiLineHazard,
	createMRiTask,
	createMRiTaskInspection,
	createMRiTaskInfo,
	createMRiLineInspMp,
	createMRiTaskInspMp,
	createMRiTaskInspectionAbnormal,
	createMRiTaskDown,
	createMRiTaskUpload,
	createMCpisAppCollectStatus,
	insertMRiCode,
	insertMRiCodeBatch,
	insertMEqMpData,
	insertMEqMpDataBatch,
	insertMRiLineHazard,
	insertMRiTask,
	insertMRiTaskInspection,
	insertMRiTaskInspectionBatch,
	insertMRiTaskInfo,
	insertMRiLineInspMp,
	insertMRiLineInspMpBatch,
	insertMRiTaskInspMp,
	insertMRiTaskInspMpBatch,
	insertMRiTaskInspectionAbnormal,
	insertMRiTaskInspectionAbnormalBatch,
	insertMRiTaskDown,
	insertMRiTaskUpload,
	insertMCpisAppCollectStatus,
	taskPageTypeList,
	getTaskAreaStat,
	getInspectionHistory,
	getMpDataList
}
